package com.tools.pgsql;

import com.bean.SlDataDTO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SLSqlHelper {

    private SlDataDTO dataDTO;

    public void contectSql(){
        Connection c = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            // 连接数据库
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection(
                    "jdbc:postgresql://23.159.250.160:5432/bmzyc",
                    "wsdsjzx",
                    "563%ymwdCg@z");
            c.setAutoCommit(false); // 手动管理事务

            System.out.println("连接数据库成功！");

            String sql1 = "SELECT *\n" +
                    "\tFROM wsslj.cy_jsc_water_regime\n" +"where equipment_name='千丈岩水库'"+
                    "\torder by last_time desc;";
            statement = c.prepareStatement(sql1);
            rs = statement.executeQuery();
            rs.next();
            String row_id = rs.getString(1);
            String equipment_name = rs.getString(3);
            String val = rs.getString(5);
            String warn_threshold = rs.getString(7);
            String last_time = rs.getString(9);
            dataDTO = new SlDataDTO();
            System.out.println(row_id+ ',' + equipment_name+ ',' + val + ',' +last_time + ',' + warn_threshold);
            rs.close();
            statement.close();
            c.close();
//            double value = Double.parseDouble(val);
            dataDTO.setName(equipment_name);
            dataDTO.setCurDeep(val);
            dataDTO.setTimes(last_time);
       } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }

    }

}
